package com.yaoyong.yy_demo.module.rabbitmq.work;


import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import com.yaoyong.yy_demo.module.rabbitmq.util.ConnectionUtil;

public class Producer {

    private final static String QUEUE_NAME = "test_queue_work";

    public static void main(String[] argv) throws Exception {
        // 获取到连接以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        /**
         * 注意:RabbitMQ不允许您使用不同的参数重新定义现有队列
         * 生产者和消费者设置需要保持一致
         */
        // 声明队列并持久化
        boolean durable = true;
        channel.queueDeclare(QUEUE_NAME, durable, false, false, null);

        //消息持久化
        AMQP.BasicProperties basicProperties = MessageProperties.PERSISTENT_TEXT_PLAIN;
        for (int i = 0; i < 20; i++) {
            // 消息内容
            String message = "" + i;
            channel.basicPublish("", QUEUE_NAME, basicProperties, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");

            Thread.sleep(i * 10);
        }

        channel.close();
        connection.close();
    }
}